package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.SensorImgController;
import com.antoco.operation.bigscreen.model.dto.SensorImgDTO;
import com.antoco.operation.bigscreen.model.dto.SensorImgParamDTO;
import com.antoco.operation.bigscreen.model.entity.SensorImg;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.SensorImgMapper;
import com.antoco.operation.bigscreen.mapper.SensorImgParamMapper;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SensorImgService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/sensor-img")
@RestController
public class SensorImgControllerImpl implements SensorImgController {

    @Autowired
    private SensorImgService sensorImgService;

    @Autowired
    private SensorImgParamMapper sensorImgParamMapper;

    @Autowired
    private SensorImgMapper sensorImgMapper;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public SensorImgParamDTO save(@RequestBody SensorImgParamDTO sensorImgDTO) {
        SensorImg sensorImg = sensorImgParamMapper.asEntity(sensorImgDTO);
        return sensorImgParamMapper.asDTO(sensorImgService.save(sensorImg));
    }

    @Override
    @PostMapping("/save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<SensorImgParamDTO> saveLists(@Validated @RequestBody ValidList<SensorImgParamDTO> sensorImgParamDTO) {
        sensorImgService.save(sensorImgParamMapper.asEntityList(sensorImgParamDTO));
        return sensorImgParamDTO;
    }

    @Override
    @GetMapping("/{id}")
    public List<SensorImgDTO> findBySensorId(@PathVariable("id") String id) {
        List<SensorImg> sensorImgs = sensorImgService.findAllBySensorId(Long.valueOf(id));
        return sensorImgMapper.asDTOList(sensorImgs);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        sensorImgService.deleteById(id);
    }

    @Override
    @GetMapping
    public List<SensorImgDTO> list() {
        return sensorImgMapper.asDTOList(sensorImgService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<SensorImgDTO> pageQuery(Pageable pageable) {
        Page<SensorImg> sensorImgPage = sensorImgService.findAll(pageable);
        List<SensorImgDTO> dtoList = sensorImgPage
                .stream()
                .map(sensorImgMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, sensorImgPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public SensorImgParamDTO update(@RequestBody SensorImgParamDTO sensorImgDTO, @PathVariable("id") String id) {
        SensorImg sensorImg = sensorImgParamMapper.asEntity(sensorImgDTO);
        return sensorImgParamMapper.asDTO(sensorImgService.update(sensorImg, id));
    }
}